// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TickSizeRule.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Org.OpenTrader.Framework.Entities;
    using Org.OpenTrader.Framework.Forge.Enums;
    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The tick size rule.
    /// </summary>
    [Serializable]
    public class TickSizeRule : ITickSizeRule
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TickSizeRule"/> class. 
        /// Constructor
        /// </summary>
        /// <param name="dbTicksizerule">
        /// </param>
        public TickSizeRule(DBTickSizeRule dbTicksizerule)
        {
            // Unique Identifier
            this.ID = dbTicksizerule.ID;

            // Rules table
            this.TickSizeTable = new SortedDictionary<double, double>(new Comparer());

            // Name
            this.Name = dbTicksizerule.Name;

            // Load all fields and populate the local table
            dbTicksizerule.DBTickSizeRuleField.Load();
            foreach (var f in dbTicksizerule.DBTickSizeRuleField)
            {
                this.TickSizeTable[(double)f.Price] = (double)f.TickSize;
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Unique identifier
        /// </summary>
        public Guid ID { get; set; }

        /// <summary>
        /// Name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets PriceTable.
        /// </summary>
        public double[] PriceTable
        {
            get
            {
                return this.TickSizeTable.Keys.ToArray();
            }
        }

        /// <summary>
        /// Sorted list of the Price/tick size (table)
        /// </summary>
        public IDictionary<double, double> TickSizeTable { get; set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// Return Name
        /// </summary>
        /// <returns>
        /// The to string.
        /// </returns>
        public override string ToString()
        {
            return this.Name;
        }

        #endregion

        #region Implemented Interfaces

        #region ITickSizeRule

        /// <summary>
        /// Round the price using the tick size
        /// </summary>
        /// <param name="price">
        /// </param>
        /// <param name="type">
        /// </param>
        /// <returns>
        /// The round.
        /// </returns>
        public double Round(double price, ERoundType type)
        {
            var p = price;
            var tick = this.Tick(price);

            if (tick != 0.0)
            {
                price = tick * ((int)((double)(price / tick) + Constants.Zero));
            }
            else
            {
                price = 0.0;
            }

            if (type == ERoundType.Up)
            {
                if ((Math.Abs(price - p) < tick) && (Math.Abs(price - p) != 0.0))
                {
                    price += tick;
                }
            }

            return price;
        }

        /// <summary>
        /// Round the size
        /// </summary>
        /// <param name="size">
        /// </param>
        /// <returns>
        /// The round.
        /// </returns>
        ////public int Round(int size)
        ////{
        ////    if (this.LotSize <= 0)
        ////    {
        ////        return 0;
        ////    }
        ////    else
        ////    {
        ////        return (int)(size / this.LotSize) * this.LotSize;
        ////    }
        ////}

        /// <summary>
        /// Get the tick price to round the price
        /// </summary>
        /// <param name="price">
        /// </param>
        /// <returns>
        /// The tick.
        /// </returns>
        public double Tick(double price)
        {
            // use the table to get the price tick
            foreach (var t in this.TickSizeTable.Keys)
            {
                if (price >= t)
                {
                    return this.TickSizeTable[t];
                }
            }

            return 0.0;
        }

        #endregion

        #endregion

        /// <summary>
        /// How to sort a TickSizeRule
        /// </summary>
        internal class Comparer : System.Collections.Generic.IComparer<double>
        {
            #region Implemented Interfaces

            #region IComparer<double>

            /// <summary>
            /// The compare.
            /// </summary>
            /// <param name="x">
            /// The x.
            /// </param>
            /// <param name="y">
            /// The y.
            /// </param>
            /// <returns>
            /// The compare.
            /// </returns>
            public int Compare(double x, double y)
            {
                return (int)((y - x) * 100);
            }

            #endregion

            #endregion
        }
    }
}